//
//  MBCompetationDetailsController.m
//  MiBaoTu
//
//  Created by admin on 16/11/11.
//  Copyright © 2016年 com.caesartech15. All rights reserved.
//

#import "MBCompetationDetailsController.h"
#import "MBCompetationContentController.h"
#import "MBComDetailController.h"

#import "MBSpaceMyParentChildShowTabbarView.h"

@interface MBCompetationDetailsController ()<MBSpaceMyParentChildShowTabbarViewDelegate,UIScrollViewDelegate> {
    MBSpaceMyParentChildShowTabbarView              *_tabbarView;
    UIScrollView                                    *_scrollView;
    NSInteger                                       selectTabbarIndex;
    NSMutableArray                                  *_ViewCtrlArr;
}

@end
@implementation MBCompetationDetailsController

- (void)viewDidLoad
{
    [super viewDidLoad];
    selectTabbarIndex = 0;
    //this is show UI with data for Competation
    [self onSetNavBar];
    
    [self onCreateUI];
    
    [self addController];
}

#pragma mark - UI
- (void)onSetNavBar
{
    [self setBackItem];
    [self setTitleView:@"比赛名称"];
}

- (void)onCreateUI
{
    UIView *headerView = [self addHeaderView];

    _ViewCtrlArr = [[NSMutableArray alloc]initWithObjects:@"热门",@"推荐",@"最新",@"获奖", nil];
    _tabbarView = [[MBSpaceMyParentChildShowTabbarView alloc]initWithFrame:CGRectMake(0, headerView.bottom, SCREEN_WIDTH, ISIPAD?100*PERCEN:76*PERCEN) withCount:_ViewCtrlArr withbgColor:[UIColor blackColor] withSelectColor:TINK_BG_COLOR];
    _tabbarView.delegate = self;
    [self.view addSubview:_tabbarView];
    
    //全局滚动
    _scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, _tabbarView.bottom, SCREEN_WIDTH, self.view.height-_tabbarView.bottom)];
    _scrollView.backgroundColor = [UIColor clearColor];
    _scrollView.pagingEnabled = YES;
    _scrollView.delegate = self;
    _scrollView.bounces = YES;
    [self.view addSubview:_scrollView];
    _scrollView.contentSize = CGSizeMake(_scrollView.width*_ViewCtrlArr.count, _scrollView.height);

}

- (UIView *)addHeaderView
{
    UIView *headerView = [[UIView alloc]initWithFrame:CGRectMake(0, NAV_HEIGHT, SCREEN_WIDTH, 288*PERCEN)];
    if (ISIPAD) {
        headerView.height=500*PERCEN;
    }
    headerView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:headerView];
    
    //背景
    UIImageView *imgHeaderView = [[UIImageView alloc]initWithFrame:headerView.bounds];
    imgHeaderView.backgroundColor = [UIColor clearColor];
    NSString *picPath = [_dataDic objectForKey:@"PicPath"];
    NSString *picPath2 = [_dataDic objectForKey:@"PicPath2"];
    
    if (ISIPAD) {
        if (![picPath2 isKindOfClass:[NSNull class]]) {
            [imgHeaderView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",MUSIC_PLAY_Url,picPath2] ] placeholderImage:[UIImage imageNamed:@"bisai_moren"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
                imgHeaderView.image = [self getCenterImg:image withImgView:imgHeaderView];
            }];
        } else {
            imgHeaderView.image = [UIImage imageNamed:@"bisai_moren"];
        }
        

        
    }else{
        if (![picPath isKindOfClass:[NSNull class]]) {
            [imgHeaderView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",MUSIC_PLAY_Url,picPath] ] placeholderImage:[UIImage imageNamed:@"jiatu"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
                imgHeaderView.image = [self getCenterImg:image withImgView:imgHeaderView];
            }];
        } else {
            imgHeaderView.image = [UIImage imageNamed:@"jiatu"];
        }
        

    }
    [headerView addSubview:imgHeaderView];
    
    UIImage *imgs = [UIImage imageNamed:@"right_bg"];
    UIImageView *bgView = [[UIImageView alloc]initWithFrame:CGRectMake(0, headerView.height-headerView.width*imgs.size.height/imgs.size.width, headerView.width, headerView.width*imgs.size.height/imgs.size.width)];
    bgView.backgroundColor = [UIColor clearColor];
    bgView.image = imgs;
    [headerView addSubview:bgView];
    
    UIButton *bgBt = [UIButton buttonWithType:UIButtonTypeCustom];
    bgBt.frame = bgView.bounds;
    [bgBt addTarget:self action:@selector(onTapForWhat) forControlEvents:UIControlEventTouchUpInside];
    [headerView addSubview:bgBt];
    
    //比赛名称
    UILabel *name = [[UILabel alloc]initWithFrame:CGRectMake(24*PERCEN, headerView.height-20-10*PERCEN, headerView.width-24*PERCEN-60-24*PERCEN, 20)];
    if (ISIPAD) {
        name.frame=CGRectMake(80*PERCEN, headerView.height-120*PERCEN, 400*PERCEN, 40*PERCEN);
    }
    name.textColor = [UIColor whiteColor];
    name.textAlignment = NSTextAlignmentLeft;
    name.font = [UIFont systemFontOfSize:13.];
    if (ISIPAD) {
         name.font = [UIFont systemFontOfSize:40*PERCEN];
          name.textColor = UIColorFromHex(0xffffff);
    }
    name.text = [NSString stringWithFormat:@"%@",[_dataDic objectForKey:@"ActivityName"]];
    [headerView addSubview:name];
    
    //比赛简介
    UILabel *content = [[UILabel alloc]initWithFrame:CGRectMake(24*PERCEN, name.top-20-4*PERCEN, name.width, 20)];
    if (ISIPAD) {
        content.frame=CGRectMake(name.left, name.bottom+20*PERCEN, 400*PERCEN, 30*PERCEN);
    }
    content.textColor = [UIColor whiteColor];
    content.textAlignment = NSTextAlignmentLeft;
    content.font = [UIFont systemFontOfSize:15.];
    if (ISIPAD) {
        content.font = [UIFont systemFontOfSize:30*PERCEN];
        content.textColor = UIColorFromHex(0xffffff);
    }
    content.text = [NSString stringWithFormat:@"%@",[_dataDic objectForKey:@"Content"]];
    content.numberOfLines = 1;
    [headerView addSubview:content];
    
    UIButton *rBt = [UIButton buttonWithType:UIButtonTypeCustom];
    rBt.frame = bgView.frame;
    [rBt addTarget:self action:@selector(onTapForWhat) forControlEvents:UIControlEventTouchUpInside];
    [headerView addSubview:rBt];

    
    //右
    UIButton *rightBt = [UIButton buttonWithType:UIButtonTypeCustom];
    [rightBt setImage:[UIImage imageNamed:@"right_arrow"] forState:0];
    [rightBt setImage:[UIImage imageNamed:@"right_arrow"] forState:1];
    if (!ISIPAD) {
        [rightBt setImageEdgeInsets:UIEdgeInsetsMake(0, 60-24*PERCEN, 0, 0)];
        rightBt.frame = CGRectMake(headerView.width-60-24*PERCEN, headerView.height-40-8*PERCEN, 60, 40);
    }
    else{
         rightBt.frame = CGRectMake(headerView.width-80*PERCEN-60*PERCEN, headerView.height-80*PERCEN, 60*PERCEN, 40*PERCEN);
    }
    
    [rightBt addTarget:self action:@selector(onTapForWhat) forControlEvents:UIControlEventTouchUpInside];
    [headerView addSubview:rightBt];
    
    return headerView;
}

//添加子控件
- (void)addController
{
    for (int i=0; i<_ViewCtrlArr.count; i++) {
        MBCompetationContentController *show = [[MBCompetationContentController alloc]init];
        if (i == 0) {
            show.type = CompetationContentType_Hot;
        }
        else if (i == 1) {
            show.type = CompetationContentType_TuiJian;
        }
        else if (i == 2) {
            show.type = CompetationContentType_New;
        }
        else if (i == 3) {
            show.type = CompetationContentType_Peceo;
        }
        
        show.dataDic = _dataDic;
        show.isStart = _isStart;
        
        [self addChildViewController:show];
        
        if (i == 0) {
            show.view.frame = CGRectMake(0, 0, _scrollView.width, _scrollView.height);

            [_scrollView addSubview:show.view];
        }

    }
}

#pragma mark - actions
#pragma mark - 分栏选择器
- (void)onSelectItemForView:(MBSpaceMyParentChildShowTabbarView *)view withIndex:(NSInteger)selectIndex
{
    NSInteger index = _scrollView.contentOffset.x / _scrollView.width;
    if (index != selectIndex) {
        selectTabbarIndex = selectIndex;
        [_scrollView setContentOffset:CGPointMake(selectIndex*_scrollView.width, 0) animated:YES];
    }
}

#pragma mark - 滚动
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    NSInteger index = _scrollView.contentOffset.x / _scrollView.width;
    if (index != selectTabbarIndex) {
        selectTabbarIndex = index;
        [_tabbarView onUPdateUIForSelectIndex:index];
        
        [self scrollViewDidEndScrollingAnimation:_scrollView];
    }
}

- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
{
    NSInteger index = _scrollView.contentOffset.x / _scrollView.width;

    
    MBCompetationContentController *show = self.childViewControllers[index];
    if (show.view.superview) {
        return;
    }
    show.view.frame = scrollView.bounds;
    [_scrollView addSubview:show.view];
}

#pragma mark - 图片上按钮
- (void)onTapForWhat
{
   
    
    MBComDetailController *comDetailVC = [[MBComDetailController alloc] init];
    comDetailVC.title = @"比赛简介";
    comDetailVC.contentName = [_dataDic objectForKey:@"HtmlCon"];
    [self.navigationController pushViewController:comDetailVC animated:YES];
}

@end
